import Vue from 'vue'
import Vuex from 'vuex'

// 安装插件
Vue.use(Vuex)

// 创建 Store对象
const store = new Vuex.Store({
  state: {
    //购物车数据
    cartList: []
  },
  mutations: {
    addCart(state, payload) {
      // 添加购物车数据
      //   state.cartList.push(payload)
      // //   判断商品是否重复



      let oldProduct = state.cartList.find(item => item.iid == payload.iid)

      if (oldProduct) {
        oldProduct.count += 1
      } else {
        payload.count = 1
        payload.checked = true
        state.cartList.push(payload)
        console.log(state)
      }

    }
  },
  getters: {
    cartLength(state) {
      return state.cartList.length
    },
    cartList(state) {
      return state.cartList
    },
  }
})


export default store
